/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.dao.hibernate;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import unilearn.dao.RoleDAO;
import unilearn.vo.Role;

/**
 *
 * @author Yat
 */
public class RoleDAOImpl extends HibernateDAO implements RoleDAO {

    public RoleDAOImpl() {
        super();
    }

    public RoleDAOImpl(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<Role> getAll() throws Exception {
        return super.getAll(Role.class.getName());
    }

    @Override
    public Role get(int id) throws Exception {
        Session s = sessionFactory.openSession();
        Transaction t = s.beginTransaction();

        try {
            Query query = s.createQuery("from " + Role.class.getName() + " r where r.id = " + id);
            List<Role> rs = query.list();
            if ((rs == null) || (rs.isEmpty())) {
                return null;
            }

            return rs.get(0);
        } finally {
            t.commit();
            s.close();
        }
    }
    
      @Override
    public List<Role> find(Role role) throws Exception {
        return (List<Role>) super.find(role);
    }

    @Override
    public Role insert(Role role) throws Exception {
        super.insert(role);
        return role;
    }

    @Override
    public Role update(Role role) throws Exception {
        super.update(role);
        return role;
    }

    @Override
    public void delete(Role role) throws Exception {
        super.delete(role);
    }
}
